package com.cjq.allservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cjq.allservice.entity.Images;
import com.cjq.allservice.service.ImagesService;
import com.cjq.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjq
 * @since 2022-01-11
 */
@RestController
@RequestMapping("/allservice/images")
@CrossOrigin
public class ImagesController {
    @Autowired
    private ImagesService imagesService;


    @ApiOperation("添加商品图片")
    @PostMapping("/addImages/{goodsId}")
    public R addImages(@PathVariable String goodsId,@RequestBody ArrayList<String> images){
        Images images1 = new Images();
        images1.setGoodsId(goodsId);
        Boolean flag=true;
        for (int i=0;i<images.size();i++) {
            images1.setImageUrl(images.get(i));
            boolean save = imagesService.save(images1);
            if (save==false){
                flag=false;
            }
        }
        if (flag==true){
            return R.succ().message("添加成功！");
        }else {
            return R.fail().message("添加失败！");
        }
    }

    @ApiOperation("删除商品图片")
    @PostMapping("/deleteImagesById")
    public R deleteImagesById(@RequestParam String imagesId){
        boolean b = imagesService.removeById(imagesId);
        if (b==true){
            return R.succ().message("删除成功！");
        }else {
            return R.fail().message("删除失败！");
        }
    }

    @ApiOperation("根据商品id获取所有图片")
    @GetMapping("getAllImagesByGoodsId")
    public R getAllImagesByGoodsId(@RequestParam String goodsId){
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",goodsId);
        List list = imagesService.list(wrapper);
        return R.succ().data("imagesList",list);
    }

}

